<template>
  <div class="app-container">
    <!-- 顶部导航栏 -->
    <header class="header">
      <div class="logo">
        <span class="iconify" data-icon="mdi:medal" style="color: white;"></span>
        运动会管理系统
      </div>
      <div class="flex-grow"></div>
      <div class="flex items-center gap-6">
        <div class="flex items-center gap-2">
          <img id="img-head" src="img/jstu1.webp">
          <span class="text-white text-sm">{{ realName || '管理员' }}</span>
        </div>
        <div class="flex items-center gap-2">
          <a href="#" class="denglu" @click.prevent="handleLogout">退出登录</a>
        </div>
      </div>
    </header>

    <!-- 左侧导航 -->
    <nav class="sidebar">
      <div class="menu-item active">
        <span class="iconify" data-icon="mdi:cog"></span>
        <router-link to="/admin">系统设置</router-link>
      </div>
      <div class="menu-item">
        <span class="iconify" data-icon="mdi:account-group"></span>
        <router-link to="/admin/users">用户管理</router-link>
      </div>
      <div class="menu-item">
        <span class="iconify" data-icon="mdi:clipboard-list"></span>
        <router-link to="/admin/projects">项目管理</router-link>
      </div>
      <div class="menu-item">
        <span class="iconify" data-icon="mdi:calendar-clock"></span>
        <router-link to="/admin/schedule">赛程管理</router-link>
      </div>
      <div class="menu-item">
        <span class="iconify" data-icon="mdi:whistle"></span>
        <router-link to="/admin/score-record">成绩录入</router-link>
      </div>
      <div class="menu-item">
        <span class="iconify" data-icon="mdi:chart-line"></span>
        <router-link to="/admin/score-display">成绩公示</router-link>
      </div>
      <div class="menu-item">
        <span class="iconify" data-icon="mdi:file-text"></span>
        <router-link to="/admin/enrollment">报名管理</router-link>
      </div>
      <div class="menu-item">
        <span class="iconify" data-icon="mdi:archive"></span>
        <router-link to="/admin/backup">数据备份与恢复</router-link>
      </div>
      <div class="menu-item">
        <span class="iconify" data-icon="mdi:file-document"></span>
        <router-link to="/admin/files">文件管理</router-link>
      </div>
      <div class="menu-item">
        <span class="iconify" data-icon="mdi:certificate"></span>
        <router-link to="/admin/certificates">证书管理</router-link>
      </div>
      <div class="menu-item">
        <span class="iconify" data-icon="mdi:cog"></span>
        <router-link to="/admin/system-config">系统配置</router-link>
      </div>
      <div class="menu-item">
        <span class="iconify" data-icon="mdi:format-list-bulleted"></span>
        <router-link to="/admin/dict-type">字典管理</router-link>
      </div>
      <div class="menu-item">
        <span class="iconify" data-icon="mdi:history"></span>
        <router-link to="/admin/operation-log">操作日志</router-link>
      </div>
      <div class="menu-item">
        <span class="iconify" data-icon="mdi:alert-circle"></span>
        <router-link to="/admin/exception-handling">异常处理</router-link>
      </div>
    </nav>

    <!-- 主内容区 -->
    <main class="content">
      <div class="main-content-header">
        <h1 class="text-2xl font-medium">数据看板</h1>
      </div>

      <div class="dashboard-grid">
        <!-- 数据概览卡片 -->
        <div class="card">
          <div class="flex items-center justify-between">
            <h2 class="text-lg font-medium">用户总数</h2>
            <div class="iconify text-blue-500" data-icon="mdi:account-group"></div>
          </div>
          <p class="mt-4 text-3xl font-bold">2,148</p>
        </div>

        <div class="card">
          <div class="flex items-center justify-between">
            <h2 class="text-lg font-medium">赛事总数</h2>
            <div class="iconify text-orange-500" data-icon="mdi:run"></div>
          </div>
          <p class="mt-4 text-3xl font-bold">86</p>
        </div>

        <div class="card">
          <div class="flex items-center justify-between">
            <h2 class="text-lg font-medium">待办事项</h2>
            <div class="iconify text-purple-500" data-icon="mdi:calendar-alert"></div>
          </div>
          <p class="mt-4 text-3xl font-bold">12</p>
          <div class="flex items-center mt-1">
            <span class="iconify text-red-500" data-icon="mdi:alert-circle"></span>
            <span class="text-red-500 text-sm ml-1">3项紧急</span>
          </div>
          <div class="quick-actions">
            <button class="btn-alert text-sm">查看详情</button>
          </div>
        </div>
      </div>

      <div class="content-section flex">
        <div class="card1">
          <div class="flex justify-between items-center mb-4">
            <h2 class="text-lg font-medium">用户管理列表</h2>
            <div class="flex gap-3">
              <button class="btn-primary">新增用户</button>
            </div>
          </div>
          <div class="table-container">
            <table class="table">
              <thead>
                <tr>
                  <th>用户ID</th>
                  <th>姓名</th>
                  <th>角色</th>
                  <th>联系方式</th>
                  <th>负责赛事</th>
                  <th>状态</th>
                  <th>操作</th>
                </tr>
              </thead>
              <tbody>
                <tr>
                  <td>U1023</td>
                  <td>张伟</td>
                  <td>赛事管理员</td>
                  <td>138****5678</td>
                  <td>男子100米</td>
                  <td><span class="status-success">活跃</span></td>
                  <td class="table-actions">
                    <button class="btn-secondary text-sm">编辑</button>
                    <button class="text-sm">查看</button>
                  </td>
                </tr>
                <tr>
                  <td>U2047</td>
                  <td>李娜</td>
                  <td>裁判</td>
                  <td>139****2345</td>
                  <td>跳远</td>
                  <td><span class="status-success">活跃</span></td>
                  <td class="table-actions">
                    <button class="btn-secondary text-sm">编辑</button>
                    <button class="text-sm">查看</button>
                  </td>
                </tr>
                <tr>
                  <td>U3056</td>
                  <td>王建国</td>
                  <td>系统管理员</td>
                  <td>150****7890</td>
                  <td>全部项目</td>
                  <td><span class="status-success">活跃</span></td>
                  <td class="table-actions">
                    <button class="btn-secondary text-sm">编辑</button>
                    <button class="text-sm">查看</button>
                  </td>
                </tr>
                <tr>
                  <td>U4089</td>
                  <td>陈芳</td>
                  <td>数据录入员</td>
                  <td>136****1122</td>
                  <td>游泳项目</td>
                  <td><span class="status-warning">繁忙</span></td>
                  <td class="table-actions">
                    <button class="btn-secondary text-sm">编辑</button>
                    <button class="text-sm">查看</button>
                  </td>
                </tr>
                <tr>
                  <td>U5021</td>
                  <td>赵刚</td>
                  <td>志愿者组长</td>
                  <td>152****3344</td>
                  <td>安保后勤</td>
                  <td><span class="status-success">活跃</span></td>
                  <td class="table-actions">
                    <button class="btn-secondary text-sm">编辑</button>
                    <button class="text-sm">查看</button>
                  </td>
                </tr>
                <tr>
                  <td>U6040</td>
                  <td>马玲</td>
                  <td>裁判</td>
                  <td>137****5566</td>
                  <td>铅球比赛</td>
                  <td><span class="status-error">缺勤</span></td>
                  <td class="table-actions">
                    <button class="btn-secondary text-sm">编辑</button>
                    <button class="text-sm">查看</button>
                  </td>
                </tr>
              </tbody>
            </table>
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<script>
import { computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useAuthStore } from '@/stores/auth'

export default {
  name: 'BackgroundHome',
  setup() {
    const router = useRouter()
    const authStore = useAuthStore()

    // 从store获取用户信息
    const realName = computed(() => authStore.realName || '管理员')

    // 处理退出登录
    const handleLogout = async () => {
      try {
        await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })

        await authStore.logout()
        ElMessage.success('已退出登录')
        router.push('/login')
      } catch (error) {
        // 用户取消操作
      }
    }

    onMounted(() => {
      console.log('管理员首页加载完成')
    })

    return {
      realName,
      handleLogout
    }
  }
}
</script>

<style scoped>
* {
  font-family: 'PingFang SC', -apple-system, BlinkMacSystemFont, sans-serif;
}

body {
  background-color: #f0f2f5;
  color: #1a1a1a;
}

/* 头像框 */
#img-head {
  height: 40px;
  width: 40px;
  border-radius: 50px;
}

/* 退出登录 */
.denglu {
  color: white;
  font-size: 15px;
}

.denglu:hover {
  color: #F44336;
}

.app-container {
  max-width: 1440px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: 64px 1fr;
  min-height: 100vh;
  background-color: #F5F7FA;
}

.header {
  grid-column: 1 / -1;
  background-color: #2C5AA0;
  display: flex;
  align-items: center;
  padding: 0 24px;
}

.logo {
  font-size: 18px;
  font-weight: bold;
  color: white;
  display: flex;
  align-items: center;
  gap: 8px;
}

/* 左侧导航栏 */
.sidebar {
  background-color: white;
  padding: 16px 0;
  border-right: 1px solid #e4e7ed;
}

.menu-item {
  padding: 12px 24px;
  color: #555;
  display: flex;
  align-items: center;
  gap: 12px;
  transition: all 0.3s;
}

.menu-item.active,
.menu-item:hover {
  background-color: #e8eff9;
  color: #2C5AA0;
  border-right: 3px solid #2C5AA0;
}

.menu-item a {
  color: inherit;
  text-decoration: none;
  flex: 1;
}

.menu-item .router-link-exact-active {
  background-color: #e8eff9;
  color: #2C5AA0;
  border-right: 3px solid #2C5AA0;
}

.content {
  padding: 24px;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 24px;
}

.dashboard-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 24px;
}

/* 卡片 */
.card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  padding: 24px;
  border: 1px solid #e4e7ed;
}

.card1 {
  width: 100%;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  padding: 24px;
  border: 1px solid #e4e7ed;
}

.chart-container {
  height: 280px;
  width: 100%;
}

.table-container {
  overflow-x: auto;
  width: 100%;
}

.table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.table th {
  background-color: #f8fafc;
  text-align: left;
  padding: 12px 16px;
  font-weight: 500;
}

.table td {
  padding: 16px;
  border-bottom: 1px solid #eee;
}

.table tr:nth-child(even) {
  background-color: #fafbfc;
}

.btn-primary {
  background-color: #2C5AA0;
  color: white;
  border-radius: 4px;
  padding: 8px 16px;
  font-weight: 500;
}

.btn-secondary {
  background-color: transparent;
  border: 1px solid #2C5AA0;
  color: #2C5AA0;
  border-radius: 4px;
  padding: 8px 16px;
  font-weight: 500;
}

.btn-alert {
  background-color: #FF6B35;
  color: white;
  border-radius: 4px;
  padding: 8px 16px;
  font-weight: 500;
}

.status-success {
  color: #4CAF50;
  background: rgba(76, 175, 80, 0.1);
  padding: 4px 8px;
  border-radius: 4px;
}

.status-warning {
  color: #FFC107;
  background: rgba(255, 193, 7, 0.1);
  padding: 4px 8px;
  border-radius: 4px;
}

.status-error {
  color: #F44336;
  background: rgba(244, 67, 54, 0.1);
  padding: 4px 8px;
  border-radius: 4px;
}

.quick-actions {
  display: flex;
  gap: 12px;
  margin-top: 16px;
}

.table-actions {
  display: flex;
  gap: 8px;
}

.alert-icon {
  position: relative;
}

.alert-badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background: #FF6B35;
  color: white;
  border-radius: 50%;
  width: 18px;
  height: 18px;
  font-size: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.main-content-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 16px;
  border-bottom: 1px solid #e4e7ed;
}

.content-section {
  width: 100%;
  overflow: auto;
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 24px;
}
</style>